#include<stdio.h>
#include<stdlib.h>
#include<sys/stat.h>
#include<unistd.h>
#include<linux/stat.h>
#include<string.h>
#include<sys/types.h>

pid_t pid;
FILE *fp,*fp1;  
char buffer[1024];
#define pubpipe "public"
#define pripipe "private"

int main()
{
	
    if((fp=fopen(pubpipe,"r"))==NULL)
    {
        umask(0);
        mknod(pubpipe,S_IFIFO|0666,0);
    }
    else
        fclose(fp);
	
	pid=fork();
	while(1)
	{
		if(pid==0)
		{
			if((fp=fopen(pubpipe,"r"))==NULL)
			{
				printf("open fifo failed.\n");
				exit(1);
			}
			if(fgets(buffer,1024,fp)!=NULL)
			{
				if(strcmp(buffer,"quit")==0)
					exit(1);
				usleep(100000);
				printf("%s\n",buffer);
				fclose(fp);
			}
			else
			{
				if(ferror(fp))
				{
					printf("read fifo failed.\n");
					exit(1);
				}
			}
		}
		else 
		{
			if((fp1=fopen(pripipe,"w"))==NULL)
			{
				printf("open fifo failed.\n");
				exit(1);
			}
			scanf("%s",buffer);
			if(fputs(buffer,fp1)==EOF)
			{
				printf("fputs error!\n");
				exit(1);
			}
			fclose(fp1);
			usleep(100000);
			if(strcmp(buffer,"quit")==0)
				exit(1);
		}
	}
    return 0;
}
